package invoicesystem.dao.impl;

import invoicesystem.dao.AddressDao;
import invoicesystem.entry.Address;
import invoicesystem.util.DBHelper;

import java.util.ArrayList;

public class AddressDaoImpl  implements AddressDao {
  private   DBHelper db=new DBHelper();
    @Override
    public int insertAddress(Address address) {
        String  sql=" insert into address values (null,?,?,?,?,?,?) ";
        return db.updateinfo(sql,address.getEnterprise_id(),address.getAddressee(),address.getPhone(),address.getArea(),address.getAddress_detail(),address.getDefault_status());
    }
    @Override
    public int updateAddress(Address address) {
        String sql ="update address set addressee=?,phone=?,area=?,address_detail=?  where id=? ";
        return db.updateinfo(sql,address.getAddressee(),address.getPhone(),address.getArea(),address.getAddress_detail(),address.getId());
    }
    @Override
    public ArrayList<Address> select(Integer enterprise_id) {
        String sql=" select * from address where enterprise_id=? ";
        return  db.selectinfo(sql,Address.class,enterprise_id);
    }
    @Override
    public ArrayList<Address> selectAll() {
        String sql=" select * from address ";
        return db.selectinfo(sql,Address.class);
    }
    @Override
    public int updateStatus(Address address) {
        String sql ="update address set default_status=?  where id=? ";
        return db.updateinfo(sql,address.getDefault_status(),address.getId());
    }
    @Override
    public ArrayList<Address> selectById(int enterprise_id) {
        String sql="select * from address where enterprise_id=?";
        ArrayList<Address> list=db.selectinfo(sql,Address.class,enterprise_id);
        return list;
    }

    @Override
    public Address selectByAddress(String address) {
        String sql="select * from address where address_detail=?";
        ArrayList<Address> list=db.selectinfo(sql,Address.class,address);
        return list.get(0);
    }

    @Override
    public ArrayList<Address> selectAll(int id) {
        String sql="select * from address ";
        return db.selectinfo(sql,Address.class);
    }

    @Override
    public Address selectDefault(Integer enterprise_id) {
        ArrayList<Address> list=selectById(enterprise_id);
        ArrayList<Address> addresses=new ArrayList<>();
        for (Address a:list){
            if (a.getDefault_status().equals("默认")){
                addresses.add(a);
                System.out.println(addresses);
            }
        }
        return list.get(0);
        //return null;
    }

}
